﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using VPBS.Common;

namespace VPBS.Oracle.DAL
{
    public class StockPriceDAL
    {
        /// <summary>
        /// Hàm trả về giá thị trường của từng mã cổ phiếu
        /// </summary>
        /// <param name="Sharecode"></param>
        /// <param name="StartTime">Bắt đầu giờ giao dịch</param>
        /// <param name="StopTime">Kết thúc giờ giao dịch</param>
        /// <returns>decimal MarketPrice</returns>
        public static decimal MarketPrice(string Sharecode, int StartTime, int StopTime)
        {
            try
            {
                string minute = DateTime.Now.Minute.ToString();
                if (int.Parse(minute) < 10) minute = "0" + minute;
                int constant = int.Parse(DateTime.Now.Hour.ToString() + minute);
                if (constant >= StartTime && constant <= StopTime) //Gia khop gan nhat
                {
                    //HSX
                    string command = "select * from v_hose_quotes where symbol='" + Sharecode + "'";
                    DataSet ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                                                             CommandType.Text,
                                                             command);
                    if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                    {
                        if (Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString()) > 0)
                            return Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString());
                        else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                    }

                    //HNX
                    command = "select * from v_hnx_quotes where symbol='" + Sharecode + "'";
                    ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                                                             CommandType.Text,
                                                             command);
                    if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                    {
                        if (Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString()) > 0)
                            return Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString());
                        else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                    }

                    //Upcom
                    //command = "select * from v_upcom_quotes where symbol='" + Sharecode + "'";
                    //ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                    //                                         CommandType.Text,
                    //                                         command);
                    //if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                    //{
                    //    if (Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString()) > 0)
                    //        return Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString());
                    //    else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                    //}

                    return 0M;
                }
                else
                {
                    return ClosePrice(Sharecode);
                }

            }
            catch (Exception ex)
            {
                //Logger.Error(typeof(Market_Price), ex);
                return 0M;
            }
        }

        public static decimal ClosePrice(string Sharecode)
        {
            try
            {
                #region HSX

                string command = "select * from v_hose_quotes where symbol='" + Sharecode + "'";
                DataSet ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                                                         CommandType.Text,
                                                         command);
                if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                {
                    if (Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString()) > 0)
                        return Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString());
                    else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                }

                #endregion

                #region HNX

                #region Active

                command = "select * from v_hnx_quotes where symbol='" + Sharecode + "'";
                ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                                                 CommandType.Text,
                                                 command);
                if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                {
                    if (Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString()) > 0)
                        return Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString());
                    else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                }

                #endregion

                #region Older

                //command = "select * from v_hnx_quotes where symbol='" + Sharecode + "'";
                //ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                //                                 CommandType.Text,
                //                                 command);
                //if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                //{
                //    if (Convert.ToDecimal(ds.Tables[0].Rows[0]["average_price"].ToString()) > 0)
                //        return Convert.ToDecimal(ds.Tables[0].Rows[0]["average_price"].ToString());
                //    else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                //}

                #endregion

                #endregion

                #region UPCOM

                //command = "select * from v_upcom_quotes where symbol='" + Sharecode + "'";
                //ds = OracleHelper.ExecuteDataset(ConnectStringHelper.OracleStockBoardConnectionString,
                //                                 CommandType.Text,
                //                                 command);
                //if (ds != null && ds.Tables[0].Rows.Count > 0 && ds.Tables[0].Rows[0][0].ToString().Length > 0)
                //{
                //    if (Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString()) > 0)
                //        return Convert.ToDecimal(ds.Tables[0].Rows[0]["last_price"].ToString());
                //    else return Convert.ToDecimal(ds.Tables[0].Rows[0]["basic_price"].ToString());
                //}

                #endregion

                return 0M;
            }
            catch
            {
                //Logger.Error(typeof(MarketPrice), ex);
                return 0M;
            }
        }
    }
}
